package com.xiaoyuer.weixin.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaoyuer.third.tianju.domain.TianJuResponse;
import com.xiaoyuer.third.tianju.session.OpenTianjuSession;
import com.xiaoyuer.third.tianju.session.OpenTianjuSessionFactory;
import com.xiaoyuer.third.tianju.session.TianjuConfiguration;
import com.xiaoyuer.third.tianju.session.defaults.DefaultOpenTianjuSessionFactory;
import com.xiaoyuer.weixin.common.DataItem;
import com.xiaoyuer.weixin.common.ResultVo;
import com.xiaoyuer.weixin.config.WeChatConfigure;
import com.xiaoyuer.weixin.service.IWeChatService;
import com.xiaoyuer.weixin.utils.DataUtils;
import com.xiaoyuer.weixin.utils.LunarCalendarFestivalUtils;
import com.xiaoyuer.weixin.utils.MemoryDayUtil;
import com.xiaoyuer.weixin.vo.ChpVo;
import com.xiaoyuer.weixin.vo.WeatherVo;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class IWeChatServiceImpl implements IWeChatService {



    @Autowired
    WeChatConfigure weChatConfigure;

    @Autowired
    private WxMpService wxMpService;



    /**
     * 打印 response log
     * @param responseStr
     */
    private static void printPushLog(String responseStr){
        JSONObject jsonObject = JSONObject.parseObject(responseStr);
        String msgCode = jsonObject.getString("errcode");
        String msgContent = jsonObject.getString("errmsg");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("[ " + dateFormat.format(new Date()) + " ] : messageCode=" + msgCode + ",messageContent=" + msgContent);
    }

    @Override
    public void push() {
        try {
            // 构建模板消息
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .templateId(weChatConfigure.getTemplateId())
                    .build();
            // 1. 配置文件
            TianjuConfiguration tianjuConfiguration = new TianjuConfiguration();
            tianjuConfiguration.setApiHost(weChatConfigure.getRainbowApi());
            tianjuConfiguration.setApiKey(weChatConfigure.getRainbowKey());
            OpenTianjuSessionFactory factory = new DefaultOpenTianjuSessionFactory(tianjuConfiguration);
            // 2. 开启会话
            OpenTianjuSession openTianjuSession = factory.openSession();
            // 计算天数
            long birthdays = 0;
            if (weChatConfigure.isUseLunar()) {
                // 如果使用农历生日
                birthdays = MemoryDayUtil.calculationBirthdayByLunar(weChatConfigure.getGirlBirthDay());
            } else {
                birthdays = MemoryDayUtil.calculationBirthday(weChatConfigure.getGirlBirthDay());
            }
            templateMessage.addData(new WxMpTemplateData("birthdays", birthdays + "", "#FFA500"));

            //3.设置城市与天气信息
            LunarCalendarFestivalUtils festival = new LunarCalendarFestivalUtils();

            TianJuResponse tianQiContent = openTianjuSession.getTianQiContent("101220301", "1");
            WeatherVo weatherVo = JSONObject.parseObject(new ObjectMapper().writeValueAsString(tianQiContent.getResult()), WeatherVo.class);
            templateMessage.addData(new WxMpTemplateData("date", weatherVo.getDate(), "#00BFFF"));
            // templateMessage.addData(new WxMpTemplateData("lunar", festival.getLunarYear() + "年 " + festival.getLunarMonth() + "月" + festival.getLunarDay(), "#00BFFF"));
            //templateMessage.addData(new WxMpTemplateData("festival", festival.getLunarTerm() + " " + festival.getSolarFestival() + " " + festival.getLunarFestival(), "#00BFFF"));
            templateMessage.addData(new WxMpTemplateData("weather", weatherVo.getWeather(), "#00FFFF"));
            templateMessage.addData(new WxMpTemplateData("minTemperature", weatherVo.getLowest() + "", "#173177"));
            templateMessage.addData(new WxMpTemplateData("temp", weatherVo.getReal() + "", "#EE212D"));
            templateMessage.addData(new WxMpTemplateData("wc_day", weatherVo.getWindsc() + "", "#EE212D"));
            templateMessage.addData(new WxMpTemplateData("wd_day", weatherVo.getWind() + "", "#EE212D"));
            templateMessage.addData(new WxMpTemplateData("maxTemperature", weatherVo.getHighest() + "", "#FF6347"));
            templateMessage.addData(new WxMpTemplateData("city", weatherVo.getArea() + "", "#173177"));

            //4.设置彩虹屁
            TianJuResponse chpRes = openTianjuSession.getChpcompletions();
            ChpVo chpVo = JSONObject.parseObject(new ObjectMapper().writeValueAsString(chpRes.getResult()), ChpVo.class);
            String data = chpVo.getContent().replaceAll("\r|\n", "");
            data = chpVo.getContent().replaceAll("XXX", "阿祖");
            templateMessage.addData(new WxMpTemplateData("rainbow", data, "#FF69B4"));

            // 备注
            String remark = "";
            if (birthdays == 0) {
                remark = "\n今天是生日,生日快乐呀!";
            }
            templateMessage.addData(new WxMpTemplateData("remark", remark, "#FF1493"));
            templateMessage.setToUser(weChatConfigure.getOpenId());
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        }catch (Exception e){
            System.out.println("baocuo");
        }

    }

}
